import java.io.*;

/**
 * Created by Agrawal Bhushan on 20-04-2016.
 */

public class SimpleCodeGenerator {

    public static String[] RegArray = new String[10];

    public static void main(String[] args) throws Exception {

        LineNumberReader lnr = new LineNumberReader(new FileReader(new File("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\InterC.txt")));
        lnr.skip(Long.MAX_VALUE);
        int lines = lnr.getLineNumber() + 1, cnt = 1;
        lnr.close();

        for (int i = 0; i < RegArray.length; i++) {
            RegArray[i] = "";
        }
        BufferedReader br = new BufferedReader(new FileReader("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\InterC.txt"));
        FileWriter fw = new FileWriter(new File("G:\\BE - LAST SEM\\bka\\Labs\\CD Lab\\2\\FinalCode.txt"));
        String interCode = "";
        String[] code = new String[5];
        int reg1 = 1000;

        while ((interCode = br.readLine()) != null) {

            code = interCode.split(" ");
            System.out.println("\n" + interCode + "\n");
            String toWrite = "";
            cnt++;

            String operand1 = code[2], operand2 = "";

            if (code.length > 3) {
                //System.out.println("Operand 1 : " + operand1);
                reg1 = getReg(operand1);
                operand2 = code[4];
                if (reg1 != -1 && reg1 < 10) {
                    fw.write("MOV " + operand1 + ", R" + reg1 + "\n");
                    System.out.println("MOV " + operand1 + ", R" + reg1);
                }
                switch (code[3]) {
                    case "+":
                        if (reg1 < 10) {
                            toWrite = "ADD " + operand2 + ", R" + reg1 + "\n";
                            System.out.println("ADD " + operand2 + ", R" + reg1);
                        } else if (reg1 >= 10) {
                            toWrite = "ADD " + operand2 + ", R" + (reg1 - 10) + "\n";
                            System.out.println("ADD " + operand2 + ", R" + (reg1 - 10));
                        }
                        break;

                    case "-":
                        if (reg1 < 10) {
                            toWrite = "SUB " + operand2 + ", R" + reg1 + "\n";
                            System.out.println("SUB " + operand2 + ", R" + reg1);
                        } else if (reg1 >= 10) {
                            toWrite = "SUB " + operand2 + ", R" + (reg1 - 10) + "\n";
                            System.out.println("SUB " + operand2 + ", R" + (reg1 - 10));
                        }
                        break;

                    case "*":
                        if (reg1 < 10) {
                            toWrite = "MULT " + operand2 + ", R" + reg1 + "\n";
                            System.out.println("MULT " + operand2 + ", R" + reg1);
                        } else if (reg1 >= 10) {
                            toWrite = "MULT " + operand2 + ", R" + (reg1 - 10) + "\n";
                            System.out.println("MULT " + operand2 + ", R" + (reg1 - 10));
                        }
                        break;

                    case "/":
                        if (reg1 < 10) {
                            toWrite = "DIV " + operand2 + ", R" + reg1 + "\n";
                            System.out.println("DIV " + operand2 + ", R" + reg1);
                        } else if (reg1 >= 10) {
                            toWrite = "DIV " + operand2 + ", R" + (reg1 - 10) + "\n";
                            System.out.println("DIV " + operand2 + ", R" + (reg1 - 10));
                        }
                }
                //System.out.println("to REG : " + code[0]);
                if (reg1 < 10) {
                    RegArray[reg1] = code[0];
                } else if (reg1 >= 10) {
                    RegArray[reg1 - 10] = code[0];
                }
                fw.write(toWrite);
            }
            if (code.length == 3) {
                if (reg1 < 10) {
                    fw.write("MOV R" + reg1 + ", " + code[0] + "\n");
                    System.out.println("MOV R" + reg1 + ", " + code[0]);
                } else if (reg1 >= 10) {
                    fw.write("MOV R" + (reg1 - 10) + ", " + code[0] + "\n");
                    System.out.println("MOV R" + (reg1 - 10) + ", " + code[0]);
                }
            }
        }
        fw.close();
    }

    private static int getReg(String code) {
        System.out.println("Register Contents : ");
        for (int i = 0; i < RegArray.length; i++) {
            if (RegArray[i] != "") {
                System.out.println("R" + i + " : " + RegArray[i] + "\t");
            }
        }
        System.out.println();
        for (int i = 0; i < RegArray.length; i++) {
            if (RegArray[i].contains(code)) {
                return i + 10;
            }
        }
        for (int i = 0; i < RegArray.length; i++) {
            if (RegArray[i] == "") {
                RegArray[i] = code;
                return i;
            }
        }
        return -1;
    }
}
